﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Text.RegularExpressions;

namespace 高频打印
{
    public partial class FormUser : Form
    {
        public FormUser()
        {
            InitializeComponent();
        }

        MySqlConnection conn = new MySqlConnection("data source=localhost;database=test;user id=root;password=root;pooling=false;charset=utf8");

        private void Cleartext()
        {

            comboBox1.Visible = true;
            comboBox1.Enabled = true;
            textBox4.Visible = false;
            button3.Enabled = false;
  
            textBox1.Enabled = false;
            textBox2.Enabled = false;
            comboBox2.Enabled = false;
            comboBox3.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;
            listBox1.Enabled = false;
            button2.Enabled = false;
            comboBox3.Visible = false;
            label5.Visible = false;
            button5.Visible = false;
            button6.Visible = false;
            listBox1.Items.Clear();
            comboBox1.Items.Clear();
            textBox1.Text = "";
            textBox2.Text = "";

            comboBox3.Visible = false;
            label5.Visible = false;
            button5.Visible = false;
            button6.Visible = false;
            comboBox2.SelectedIndex = -1;
            string sql = "select userid from User_Table order by Userid";
            DataTable dtShow = new DataTable();
            MySqlDataAdapter sda = new MySqlDataAdapter(sql, conn);
            sda.Fill(dtShow);
            comboBox1.Items.Clear();
            if (dtShow.Rows.Count > 0)
            {
                for (int i = 0; i < dtShow.Rows.Count; i++)
                {
                    comboBox1.Items.Add(dtShow.Rows[i][0]);

                }
                comboBox1.SelectedIndex = -1;
            }


        }



        private void button1_Click(object sender, EventArgs e)
        {

            if (button1.Text == "添 加")
            {
                button1.Text = "确定添加";
                button2.Text = "返 回";
                button3.Enabled = false;
                comboBox1.Visible = false;
                textBox4.Visible = true;

                textBox1.Enabled = true;
                textBox2.Enabled = true;
                comboBox2.Enabled = true;
                comboBox3.Enabled = true;
                button5.Enabled = true;
                button6.Enabled = true;
                listBox1.Enabled = true;
                button2.Enabled = true;
                comboBox3.Visible = true;
                label5.Visible = true;
                button5.Visible = true;
                button6.Visible = true;
  
            }

            else if (button1.Text == "确定添加")
            {
                if (textBox4.Text.Length < 1)
                {
                    MessageBox.Show("请输入用户ID再进行添加");
                    return;
                }

                if (textBox1.Text.Length < 1)
                {
                    MessageBox.Show("请输入密码再进行添加");
                    return;
                }

                if (textBox2.Text.Length < 1)
                {
                    MessageBox.Show("请输入姓名再进行添加");
                    return;
                }


                if (listBox1.Items.Count < 1)
                {
                    MessageBox.Show("请选择门店再进行添加");
                    return;
                }


               
                if (conn.State == ConnectionState.Open)
                {

                }
                else
                {
                    conn.Open();
                }
                string sql;
                //查重
                sql = "select count(*) from User_Table where UserID='" + textBox4.Text + "'";
                DataTable dtShow1 = new DataTable();
                MySqlDataAdapter sda1 = new MySqlDataAdapter(sql, conn);
                sda1.Fill(dtShow1);
                if (Convert.ToInt32(dtShow1.Rows[0][0].ToString()) > 0)
                {
                    MessageBox.Show("你添加的用户已存在，不能重复添加");
                    return;

                }


                if (MessageBox.Show("是否要添加该用户？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
                {
                    //取listbox的值，并且逗号分开
                    string listdata = "";
                    if (listBox1.Items.Count > 0)
                    {
                        int i = 0;
                        for (i = 0; i < listBox1.Items.Count - 1; i++)
                        {
                            listdata = listdata + listBox1.Items[i] + ',';
                        }
                        listdata = listdata + listBox1.Items[i];

                        //调用添加
                        sql = "Insert into User_Table(UserID,Password,UserPower,UserName,UserDepart)Values('";
                        sql = sql + textBox4.Text + "','" + textBox2.Text + "','" + comboBox2.SelectedIndex;
                        sql = sql + "','" + textBox1.Text + "','" + listdata + "')";

                        MySqlCommand cmd = new MySqlCommand(sql, conn);
                        cmd.CommandText = sql;
                        cmd.CommandType = CommandType.Text;
                        int ret = cmd.ExecuteNonQuery();
                        MessageBox.Show("用户添加成功");

                    }
                    Cleartext();
                }
               
                button1.Text = "添 加";
                button2.Text = "修 改";
                Cleartext();
            }






            else if (button1.Text == "确定修改")
            {
        

                if (textBox1.Text.Length < 1)
                {
                    MessageBox.Show("请输入密码再进行添加");
                    return;
                }

                if (textBox2.Text.Length < 1)
                {
                    MessageBox.Show("请输入姓名再进行添加");
                    return;
                }


                if (listBox1.Items.Count < 1)
                {
                    MessageBox.Show("请选择门店再进行添加");
                    return;
                }



                if (conn.State == ConnectionState.Open)
                {

                }
                else
                {
                    conn.Open();
                }
                string sql;
                

                if (MessageBox.Show("是否要修改该用户？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
                {
                    //取listbox的值，并且逗号分开
                    string listdata = "";
                    if (listBox1.Items.Count > 0)
                    {
                        int i = 0;
                        for (i = 0; i < listBox1.Items.Count - 1; i++)
                        {
                            listdata = listdata + listBox1.Items[i] + ',';
                        }
                        listdata = listdata + listBox1.Items[i];

                        //调用添加
                        sql = "update User_Table set Password = '" + textBox2.Text + "', UserName = '" + textBox1.Text + "',";
                        sql = sql +  "UserPower = '" +comboBox2.SelectedIndex + "', UserDepart = '" + listdata + "' where UserID = '" + comboBox1.Text + "'";
                        

                        MySqlCommand cmd = new MySqlCommand(sql, conn);
                        cmd.CommandText = sql;
                        cmd.CommandType = CommandType.Text;
                        int ret = cmd.ExecuteNonQuery();
                        MessageBox.Show("用户修改成功");

                    }
                    Cleartext();
                }

                button1.Text = "添 加";
                button2.Text = "修 改";
                Cleartext();
            }



        }

        private void FormUser_Load(object sender, EventArgs e)
        {
            //填入门店代码
            string sql = "select departID from depart_Table order by departid";
            DataTable dtShow = new DataTable();
            MySqlDataAdapter sda = new MySqlDataAdapter(sql, conn);
            sda.Fill(dtShow);
            if (dtShow.Rows.Count > 0)
            {
                for (int i = 0; i < dtShow.Rows.Count; i++)
                {
                    comboBox3.Items.Add(dtShow.Rows[i][0]);

                }
            }


             sql = "select UserID from User_Table where UserPower=0 or UserPower=1 order by UserID";
            dtShow = new DataTable();
            sda = new MySqlDataAdapter(sql, conn);
            sda.Fill(dtShow);
            if (dtShow.Rows.Count > 0)
            {
                for (int i = 0; i < dtShow.Rows.Count; i++)
                {
                    comboBox1.Items.Add(dtShow.Rows[i][0]);

                }
            }

            else
            {
                MessageBox.Show("还没有用户数据，请点击“添加”按钮进行数据维护");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "修 改")
            {
                button1.Text = "确定修改";
                button2.Text = "返 回";
                button3.Enabled = false;
               
                comboBox1.Visible = true;
                textBox4.Visible = false;
                comboBox1.Enabled = false;

                textBox1.Enabled = true;
                textBox2.Enabled = true;
                comboBox2.Enabled = true;
                comboBox3.Enabled = true;
                button5.Enabled = true;
                button6.Enabled = true;
                listBox1.Enabled = true;
                button2.Enabled = true;
                comboBox3.Visible = true;
                label5.Visible = true;
                button5.Visible = true;
                button6.Visible = true;


            }

            else if (button2.Text == "返 回")
            {
                button1.Text = "添 加";
                button2.Text = "修 改";
                Cleartext();
                
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否要删除该笔资料？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
            {
                if (conn.State == ConnectionState.Open)
                {

                }
                else
                {
                    conn.Open();
                }

                string sql = "delete from User_Table where UserID = '" + comboBox1.Text + "'";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                int ret = cmd.ExecuteNonQuery();
                MessageBox.Show("数据删除成功");
                Cleartext();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1)
            {


            }
            else
            {
                string sql = "select * from User_Table where UserID='" + comboBox1.Text + "'";
                DataTable dtShow = new DataTable();
                MySqlDataAdapter sda = new MySqlDataAdapter(sql, conn);
                sda.Fill(dtShow);
                textBox1.Text = dtShow.Rows[0][3].ToString();
                textBox2.Text = dtShow.Rows[0][1].ToString();
                comboBox2.SelectedIndex = int.Parse(dtShow.Rows[0][2].ToString());
                button2.Enabled = true;
                button3.Enabled = true;
                comboBox3.Visible = false;
                label5.Visible = false;
                button5.Visible = false;
                button6.Visible = false;

                listBox1.Items.Clear();
                string departdata = dtShow.Rows[0][4].ToString();
                string[] sArray = Regex.Split(departdata, ",", RegexOptions.IgnoreCase);
                foreach (string i in sArray)
                {
                    listBox1.Items.Add(i);
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Application.Exit(); 
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
            {
                MessageBox.Show("请选择一条记录进行删除！");
            }
            else
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                if (listBox1.Items[i].ToString()==comboBox3.Text)
                {
                    MessageBox.Show("该部门已经存在，不能重复添加");
                    return;
                }
                if(comboBox3.SelectedIndex ==-1)
                {
                    MessageBox.Show("选择一个部门再点击添加");
                    return;
                }
                
            }
            listBox1.Items.Add(comboBox3.Text);

        }
    }
}

